//
//  YLIntegralRecordViewController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/19.
//  Copyright © 2017年 she1323. All rights reserved.
//  积分记录

#import "YLIntegralRecordViewController.h"
#import "YLIntegralRecordCell.h"
#import "YLGlobeHeader.h"
#import "WHNetworkTool.h"
#import "YLAccountTool.h"
#import "WHProgressHUD.h"
#import <MJRefresh.h>
#import "YLIntegralRecordModel.h"
@interface YLIntegralRecordViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) NSMutableArray *models;
@end

@implementation YLIntegralRecordViewController

- (NSMutableArray *)models
{
    if (!_models) {
        _models = [NSMutableArray array];
    }
    return _models;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLIntegralRecordCell class]) bundle:nil] forCellReuseIdentifier:[YLIntegralRecordCell cellID]];
    
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.index = 1;
        [weakSelf loadData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.index ++;
        [weakSelf loadData];
    }];
    
  //  [self.tableView.mj_header beginRefreshing];
    _index = 1;
    [self loadData];
    self.tableView.mj_footer.hidden = YES;
}


- (void)loadData
{
    NSString *url = [NSString stringWithFormat:@"%@Point/PGetPointLog", YLURL];

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:2];
    
    params[@"PageIndex"] = @(_index);
    NSInteger count = 40;
    params[@"MaxResultCount"] = @(count);
    __weak typeof(self) weakSelf = self;
    [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:params success:^(id responseObject) {
    
        
        [weakSelf endRefreshing];
        if ([responseObject[@"httpCode"] integerValue] == 200) {
            YLIntegralRecordItem *item = [YLIntegralRecordItem yy_modelWithDictionary:responseObject[@"httpResult"]];
            
            if (_index == 1) {
                [weakSelf.models removeAllObjects];
            }

            [weakSelf.models addObjectsFromArray:item.item];
            
            if (!(item.count > weakSelf.models.count)) {
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                weakSelf.tableView.mj_footer.hidden = YES;
            }
            [weakSelf.tableView reloadData];
        } else {
            [WHProgressHUD showError:responseObject[@"message"]];
        }
        
        
    } fail:^(NSError *error) {
        [weakSelf endRefreshing];
    }];
    
}

- (void)endRefreshing
{
    self.tableView.mj_footer.hidden = NO;
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLIntegralRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLIntegralRecordCell cellID]];
    cell.contentView.backgroundColor = (indexPath.row % 2 == 0) ? YLColor(248, 249, 252) : YLColor(254, 255, 255);
    cell.model = self.models[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 25;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
